package com.dongwu.manager.web;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dongwu.common.dto.Result;
import com.dongwu.common.utils.DateUtils;
import com.dongwu.common.web.BaseController;
import com.dongwu.manager.entity.DeviceAdsRecord;
import com.dongwu.manager.service.DeviceAdsRecordService;
import com.dongwu.manager.service.DeviceService;
import com.dongwu.security.entity.User;

@Controller
@RequestMapping("/manager/deviceadsrecord")
public class DeviceAdsRecordController extends BaseController<DeviceAdsRecord, String> {
	@Autowired
	private DeviceAdsRecordService deviceAdsRecordService;
	
//	@Autowired
//	private IUserService userService;
	
	@Autowired
	private DeviceService deviceService;
	
	@RequestMapping("/put")
	public String waitdistlist() {
		return this.getPagePath("put");
	}
	/**
	 * 
	 * @param remark
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/addRecord")
	@ResponseBody
	public Result addRecord(String remark, String startTime, String endTime,
			@RequestParam(value = "ids[]") String[] ids, @RequestParam(value = "pids[]") String[] pids)
			throws ParseException {
		List<DeviceAdsRecord> insdpr = new ArrayList<DeviceAdsRecord>();
		List<DeviceAdsRecord> upddpr = new ArrayList<DeviceAdsRecord>();
		for (int i = 0; i < ids.length; i++) {
			User user = deviceService.get(ids[i]).getUser();
			for (int j = 0; j < pids.length; j++) {
				DeviceAdsRecord r = new DeviceAdsRecord();
				r.setUser(user);
				r.setDeviceid(ids[i]);
				if (startTime != "") {
					r.setStartTime(DateUtils.parseDate(startTime, DateUtils.PATTERN_DATE_TIME));
				}
				if (endTime != "") {
					r.setEndTime(DateUtils.parseDate(endTime, DateUtils.PATTERN_DATE_TIME));
				}
				r.setAdvertisingid(pids[j]);
				r.setRemark(remark);
				r.setStatus(0);
				// 获取当前设备的所有投放记录
				List<DeviceAdsRecord> dprList = deviceAdsRecordService.findByDeviceId(ids[i],0);
				if (dprList.size() != 0) {
					boolean isexist = true;
					// 如果记录存在则遍历记录
					for (DeviceAdsRecord dpr : dprList) {
						// 如果已投放当前商品则更新
						if (dpr.getAdvertisingid().equals(pids[j])) {
							r.setId(dpr.getId());
							upddpr.add(r);
							isexist = false;
						}
					}
					// 如果当前设备的当前商品不存在则添加
					if (isexist) {
						insdpr.add(r);
					}
				} else {
					// 设备无投放记录时添加
					insdpr.add(r);
				}
			}
		}
		deviceAdsRecordService.batchInsert(insdpr);
		deviceAdsRecordService.batchUpdate(upddpr);
		return Result.successResult();

	}

//	/**
//	 * 获取正在投放总数
//	 * @return
//	 */
//	@RequestMapping(value = "/getCount")
//	@ResponseBody
//	public Result getCount(HttpServletRequest request) {
//		Long dprcount = null;
//		//获取当前登录的用户
//		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
//		userDetails.getUsername();
//		if(userDetails.getUsername().equals("admin")){
//			Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, SEARCH_PARAM_PREFIX);
//			super.filterDataPermission(searchParams);
//			dprcount= deviceProductRecordService.getCount(searchParams,null);
//		}else{
//			//获取当前登录的用户详情
//			User a= userService.findByLoginName(userDetails.getUsername());
//			dprcount = deviceProductRecordService.getCount(null,a.getId());
//		}
//		PagingVO paging = new PagingVO(new ArrayList<DeviceProductRecord>(),Integer.parseInt(String.valueOf(dprcount)), 0, 10);
//		return Result.successResult(paging);
//
//	}
}
